home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
F1 Licenseware
/
F1 Licenseware - Volume 1.iso
/
disks
/
089a.dms
/
089a.adf
/
TEXTS
/
CHAPTERS_21-30
/
Chapter29.txt
< prev
next >
Wrap
Text File
|
1992-03-06
|
5KB
|
150 lines
The Absolute Beginners Guide To Amos
-------------------------------------
Chapter Twenty Nine
--------------------
Imagine an enormous game like Populous or Elite without a save game option!
They would be almost unplayable without it.
I would now like to show you how to set up a load and save game feature
for your own programs.
It's a fairly straight forward affair doing this in Amos but it has NEVER
been covered anywhere, not in the Amos manual, books or magazines that I
have seen. So how is a beginner meant to know how to do this?
Don't worry, read on.
When you save a games position all you have to do is save ALL of the
variables into a file. It's as simple as that!
First, decide where you are going to put your saved game feature, the main
menu would be the usual place. Set up your menu with LOAD and SAVE in it
and when selected by the user call the Procedures _Load and _Save
respectively.
We of course, need to write the _Load and _Save procedures first.
Let's look at writing _Save first.
First of all we need to have a list of EVERY variable used in the game,
you may not actually need to save all of them, but if you are not sure about
any particular one then include it anyway.
Once you have written down all of your programs variables you are ready to
write the _Save game procedure.
To keep this example simple we will not offer the user a file selector but
we will save the games position with a predefined name. This means only one
game can ever be saved on a single disk as any subsequent saves will
overwrite the last save. This keeps things very simple and will hopefully
mean you will understand it a lot easier. Of course, once you have learnt
the basics of loading and saving variables you can expand these procedures
into your own custom routines, specific to your program.
I am going to make up an imaginary game that has only four variables in it.
The variables are:
POINTS
FRUITS (5)
MONSTER$(25)
FIRE$
As we now know all of the variables of our game we can PRINT them to a disk!
But first we must OPEN a channel. You do not need to understand how this
works, just remember that when you OPEN a channel it MUST be closed after
you have finished with it.
Open Out 1,"DF0:SAVED_GAME"
This OPENs a channel which we will call 1 for our reference. You can have
more than one channel open at a time, but I doubt you will ever find the need
to, so just use 1 all of the time. The "DF0:SAVED_GAME" is the path and
file name we will be creating on the disk. Remember the comma and the quotes
around the file name. You can put in any path or call the file anything you
wish of course.
Now we can get to work on actually saving the variables onto the disk.
PRINT #1,POINTS
This line will save the variable POINTS and it's current value to the file we
have called SAVED_GAME. The #1 tells Amos we are printing to channel one.
Now we want to save the dimensioned array FRUITS() which has five elements.
This too is fairly simple.
For A=1 To 5
Print #1,FRUITS(A)
Next A
We have used a For Next loop to put the five elements onto disk which is
stored along with POINTS in the file "SAVED_GAME"
Next we have MONSTER$() a 25 element string array. This is virtually the
same.
For A=1 To 25
Print #1,MONSTER$(A)
Next A
And lastly, we have a nice simple string variable.
PRINT #1,FIRE$
Don't forget to CLOSE channel 1,
CLOSE 1
And that's it, all the four variables and their values are all stored neatly
into one file on disk ready to restore the game to it's original position.
So, lumping it all together, here is the Procedure in full.
Procedure _SAVE
Open Out 1,"DF0:SAVED_GAME"
PRINT #1,POINTS
For A=1 To 5
Print #1,FRUITS(A)
Next A
For A=1 To 25
Print #1,MONSTER$(A)
Next A
PRINT #1,FIRE$
CLOSE 1
End Proc
Obviously to make use of this procedure in your own programs you will need
to enter the variables pertaining to your own particular program.
Now we must look at how to load back the variables into your program.
Luckily it's all very simple if you understood how the save part works.
All we need to do is substitute the PRINT statements for INPUT statements.
So all we have to do is cut and paste the _save procedure, rename it _LOAD
and change the PRINTs to INPUTs.
Procedure _LOAD
Open In 1,"df0:SAVED_GAME"
Input #1,POINTS
For A=1 To 5
Input #1,FRUITS(A)
Next A
For A=1 To 25
Input #1,MONSTER$(A)
Next A
Input #1,FIRE$
CLOSE 1
End Proc
See example29.Amos for a small demonstration.
End of Chapter Twenty Nine
^^^^^^^^^^^^^^^^^^^^^^^^^^^